package com.ruoyi.business.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.ShopBalance;
import com.ruoyi.business.mapper.ShopBalanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WithdrawalMapper;
import com.ruoyi.business.domain.Withdrawal;
import com.ruoyi.business.service.IWithdrawalService;

import javax.xml.crypto.Data;

/**
 * 提现Service业务层处理
 *
 * @author wangziyang
 * @date 2024-09-11
 */
@Service
public class WithdrawalServiceImpl implements IWithdrawalService {
    @Autowired
    private WithdrawalMapper withdrawalMapper;
    @Autowired
    private ShopBalanceMapper shopBalanceMapper;

    /**
     * 查询提现
     *
     * @param id 提现主键
     * @return 提现
     */
    @Override
    public Withdrawal selectWithdrawalById(Long id) {
        return withdrawalMapper.selectWithdrawalById(id);
    }

    @Override
    public Withdrawal selectWithdrawalByMerchantId(Long merchantId) {
        return withdrawalMapper.selectWithdrawalByMerchantId(merchantId);
    }

    /**
     * 查询提现列表
     *
     * @param withdrawal 提现
     * @return 提现
     */
    @Override
    public List<Withdrawal> selectWithdrawalList(Withdrawal withdrawal) {
        return withdrawalMapper.selectWithdrawalList(withdrawal);
    }

    /**
     * 新增提现
     *
     * @param withdrawal 提现
     * @return 结果
     */
    @Override
    public int insertWithdrawal(Withdrawal withdrawal) {
        int i = editBalance(withdrawal);
        if (i > 0) {
            /*获取当前时间*/
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = now.format(formatter);
            withdrawal.setWithdrawalRecord(formattedDateTime);
            return withdrawalMapper.insertWithdrawal(withdrawal);
        } else {
            return 0;
        }

    }

    /**
     * 修改提现
     *
     * @param withdrawal 提现
     * @return 结果
     */
    @Override
    public int updateWithdrawal(Withdrawal withdrawal) {
        if (withdrawal.getStatus() == 1) {
            /*根据商铺id得到商铺修改前的数据*/
            ShopBalance shopOld = shopBalanceMapper.selectShopBalanceByShopId(withdrawal.getMerchantId());
            /*将提现的金额和以提现金额相加得到跟新的累积提现*/
            Double withdrawalBalance = shopOld.getCashWithdrawal() + withdrawal.getCashWithdrawal();
            /*创建对象将数据放入对象中*/
            ShopBalance shopBalance = new ShopBalance();
            shopBalance.setMid(withdrawal.getMerchantId());
            shopBalance.setCashWithdrawal(withdrawalBalance);
            /*调mapper通过update跟新数据*/
            int i = shopBalanceMapper.updateShopBalanceByMid(shopBalance);
            if (i > 0) {
                return withdrawalMapper.updateWithdrawal(withdrawal);
            } else {
                return 0;
            }
        } else {
            int i = editBalance(withdrawal);
            if (i > 0) {
                return withdrawalMapper.updateWithdrawal(withdrawal);
            } else {
                return 0;
            }
        }
    }

    /**
     * 批量删除提现
     *
     * @param ids 需要删除的提现主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return withdrawalMapper.deleteBatchIds(idList);
    }

    /**
     * 删除提现信息
     *
     * @param id 提现主键
     * @return 结果
     */
    @Override
    public int deleteWithdrawalById(Long id) {
        return withdrawalMapper.deleteById(id);
    }

    private int editBalance(Withdrawal withdrawal) {
        Double balance = null;
        /*根据商铺id得到商铺修改前的数据*/
        ShopBalance shopOld = shopBalanceMapper.selectShopBalanceByShopId(withdrawal.getMerchantId());
        if (withdrawal.getStatus() == null) {
            /*将余额减去提现金额得到跟新的余额*/
            balance = shopOld.getCurrentBalance() - withdrawal.getCashWithdrawal();
            if (balance < 0){
                return 0;
            }
        } else {
            balance = shopOld.getCurrentBalance() + withdrawal.getCashWithdrawal();
        }
        /*创建对象将数据放入对象中*/
        ShopBalance shopBalance = new ShopBalance();
        shopBalance.setCurrentBalance(balance);
        shopBalance.setMid(withdrawal.getMerchantId());
        return shopBalanceMapper.updateShopBalanceByMid(shopBalance);
    }
}
